/**
 * All rights reserved by www.xmerge.cn
 */
package net.xfok.info.ecup;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

/**
 * @author YangHua
 *
 */
public class InfoReceiver extends BroadcastReceiver {
	private String TAG = "InfoReceiver";
	private volatile Context context = null;
	private final String InfoService = "net.xfok.info."+InfoConstant.infoType+".SERVICE";
	ConnectivityManager cm = null;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		this.context = context;
		String action = intent.getAction();	
		if ( ConnectivityManager.CONNECTIVITY_ACTION.equalsIgnoreCase(action) ) {
			if ( cm == null )	cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo netinfo = cm.getActiveNetworkInfo();
			if ( netinfo !=null&&netinfo.isConnected() ) {
				ServiceStart();
				Log.i( TAG, "The net was connected" );
			}
		} 
	}
	
	public void ServiceStart() {
		context.startService(new Intent(InfoService));
	}
	
	public void ServiceStop() {
		context.stopService( new Intent(InfoService) );	
	}
}
